home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4632 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  114 lines

  1. Path: zippy.cais.net!news
  2. From: mgeiger@mail.drsystems.com (Mark Geiger)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Command line Arguments
  5. Date: Tue, 06 Feb 1996 02:59:25 GMT
  6. Organization: Capital Area Internet Service info@cais.com 703-448-4470
  7. Message-ID: <4f6jpb$q7h@zippy.cais.net>
  8. References: <4f2qev$9jq@cloner3.netcom.com>
  9. Reply-To: mgeiger@mail.drsystems.com
  10. NNTP-Posting-Host: 205.252.35.151
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. buxx@ix.netcom.com(Glen 'Steve' Vandiver ) wrote:
  14.  
  15. >Hi! I have been haveing trouble with my commandline arguements. I have
  16. >it too where i can read the entire argument string after the run. No
  17. >prob. but lets say i want it to split it up like this. the first word
  18. >goes into char *user; and the rest goes into char *command;. HOW WOULD
  19. >I DO THIS? thanx! bye!
  20.  
  21. Here is a solution.  Sorry, but I haven't compiled it...there might be
  22. a typo here or there or maybe even a missing header file...but
  23. hopefully you'll get the idea <g>
  24.  
  25. #include <malloc.h>
  26. #include <string.h>
  27.  
  28. int main(int argc, char *argv[])
  29. {
  30.   char *user;
  31.   char *command;
  32.   int user_len;
  33.   int command_len;
  34.   int i;
  35.  
  36.   user = NULL;
  37.   command = NULL;
  38.   user_len = 0;
  39.   command_len = 0;
  40.  
  41.   /* Make sure that there's at least one arg to play with */
  42.  
  43.   if ( argc <= 1 )
  44.     return -1;
  45.  
  46.   /* Find out how much memory we need to allocate to hold */
  47.   /* the user name */
  48.  
  49.   user_len = strlen(argv[1]);
  50.  
  51.   /* Allocate memory for the user name, don't forget to allow for */ 
  52.   /* the terminating nul */
  53.  
  54.   user = (char *) malloc(user_len + 1);
  55.  
  56.   if ( NULL == user ) {
  57.     /* Handle no-memory error */
  58.    return -1;
  59.   }
  60.  
  61.   /* Store the user name */
  62.  
  63.   strcpy(user, argv[1]);
  64.  
  65.   /* Find out how much memory we'll need to store the remainder */
  66.   /* Remember to allow room for a delimiting space character       */
  67.   /* between each arg       */
  68.  
  69.   for ( i = 2; i < argc; i++ )
  70.     command_len += ( 1 + strlen(argv[i]));
  71.  
  72.   if ( command_len ) {
  73.  
  74.   /* Allocate the memory we need to hold the remaining args */
  75.  
  76.     command = (char *) malloc(command_len + 1);
  77.  
  78.      if ( NULL == command ) {
  79.        /* Handle no-memory error */
  80.        free(user);
  81.        return -1;
  82.     }
  83.  
  84.     /* Now copy each arg over to our string */
  85.     /* Use a space to separate them             */
  86.  
  87.    command[0] = '\0';
  88.     for ( i = 2; i < argc; i++ ) {
  89.       strcat(command, " ");
  90.       strcat(command, argv[i]);
  91.     }
  92.   }
  93.  
  94.   /* Do whatever with user and command */
  95.  
  96.   /* Clean up */
  97.  
  98.   if ( NULL != user )
  99.     free(user);
  100.  
  101.   if ( NULL != command ) 
  102.     free(command);
  103.  
  104.   return 0;
  105. }
  106.       
  107.  
  108.   
  109.  
  110.     
  111.  
  112.   
  113.  
  114.